package org.yonggan.web.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class EnumEqualsValueUtil {

    private static final String GET = "get";

    /**
     * 检索  ClassGradeEnum 枚举类
     *
     * @return
     */
    public static boolean isEqualsValue(Class cls, String type, Integer val) {
        if (type == null && "".equals(type) &&  val ==  null) {
           return  Boolean.FALSE;
        }
        Method method = null;
        try {
            final String methodName = GET + captureName(type);
            method = cls.getMethod(methodName);
            method.setAccessible(true);
            Object[] enumConstants = cls.getEnumConstants();
            for (Object cg : enumConstants) {
                Object invoke = method.invoke(cg);
                // 判断
                if (invoke.equals(val)) {
                    return  Boolean.TRUE;
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }


    //首字母大写
    public static String captureName(String name) {
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return name;
    }


    /**
     * 使用说明
     *
     * @param args
     */
    public static void main(String[] args) {

        boolean type = isEqualsValue(ClassGrade.class, "type", 6);
        System.out.println(type);

    }
}
